@echo off
setlocal enabledelayedexpansion

:: Set UTF-8 encoding
chcp 65001 >nul 2>&1

:: Set console title
title Sokit Transfer Package Tool

echo.
echo ========================================
echo    Sokit Transfer Package Script
echo ========================================
echo.

:: Check if build directory exists
if not exist "build" (
    echo [INFO] Creating build directory...
    mkdir build
    if !errorlevel! neq 0 (
        echo [ERROR] Failed to create build directory!
        pause
        exit /b 1
    )
)

:: Enter build directory
echo [INFO] Entering build directory...
cd build

:: Configure project
echo [INFO] Configuring CMake project...
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_AUTOMOC=ON
if !errorlevel! neq 0 (
    echo [ERROR] CMake configuration failed!
    echo [HINT] Please check if Qt6 is properly installed
    pause
    exit /b 1
)

:: Build project
echo [INFO] Building project...
cmake --build . --config Release
if !errorlevel! neq 0 (
    echo [ERROR] Build failed!
    echo [HINT] Please check build environment and dependencies
    pause
    exit /b 1
)

:: Check executable file
if not exist "bin\release\sokit.exe" (
    echo [ERROR] sokit.exe not found
    echo [INFO] Searching for executable files...
    dir /s /b sokit.exe 2>nul
    if !errorlevel! neq 0 (
        echo [ERROR] No sokit.exe files found
        pause
        exit /b 1
    )
)

:: Create package directory
echo [INFO] Creating package directory...
if not exist "package" (
    mkdir package
    if !errorlevel! neq 0 (
        echo [ERROR] Failed to create package directory!
        pause
        exit /b 1
    )
)
if not exist "package\sokit" (
    mkdir package\sokit
    if !errorlevel! neq 0 (
        echo [ERROR] Failed to create sokit directory!
        pause
        exit /b 1
    )
)

:: Copy executable file
echo [INFO] Copying executable file...
copy "bin\release\sokit.exe" "package\sokit\" >nul
if !errorlevel! neq 0 (
    echo [ERROR] Failed to copy executable file!
    pause
    exit /b 1
)

:: Deploy Qt dependencies
echo [INFO] Deploying Qt dependencies...
windeployqt.exe "package\sokit\sokit.exe" >nul 2>&1
if !errorlevel! neq 0 (
    echo [WARNING] Qt dependency deployment may be incomplete, but continuing...
)

:: Copy other necessary files
echo [INFO] Copying other files...
if exist "..\src\sokit\sokit.png" (
    copy "..\src\sokit\sokit.png" "package\sokit\" >nul
    echo [INFO] Application icon copied
)
if exist "..\README.md" (
    copy "..\README.md" "package\sokit\" >nul
    echo [INFO] Documentation copied
)
if exist "..\LICENSE" (
    copy "..\LICENSE" "package\sokit\" >nul
    echo [INFO] License file copied
)

:: Check package result
echo [INFO] Checking package result...
if exist "package\sokit\sokit.exe" (
    echo [SUCCESS] Executable file created
) else (
    echo [ERROR] Executable file creation failed
    pause
    exit /b 1
)

:: Display package information
echo.
echo ========================================
echo           Package Complete!
echo ========================================
echo.
echo [INFO] Package directory: %cd%\package\sokit
echo [INFO] Executable file: %cd%\package\sokit\sokit.exe
echo.
echo [HINT] You can copy the entire sokit folder to other computers
echo [HINT] Make sure target computers have necessary system dependencies
echo.

:: Ask if open package directory
set /p choice="Open package directory? (Y/N): "
if /i "!choice!"=="Y" (
    explorer "package\sokit"
)

echo.
echo [INFO] Package process completed!
pause 